package cn.jdy.lhkgaj.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Advice;

/**
 * 金点子
 * @author yym
 * @since 18032016
 */
@Service
public class McAdviceService extends BaseDao<Advice>{

	/**
	 * 按条件查询
	 * @param Notice
	 * @return
	 */
	public List<Advice> query(Advice advice, String... date) {
		if(advice == null) return super.findAll();
		//return super.find(createFilter(Notice, Notice.getiDisplayLength() > 0, date));
		if(advice.getiDisplayLength() > 0){
		  return super.find(createFilter(advice, date),advice.getiDisplayStart(),advice.getiDisplayLength());
		}
		return super.find(createFilter(advice, date));
	}

	public Long count(Advice advice, String... date) {
		return super.count(createFilter(advice, date));
	}
	
	/**
	 * 拼装条件
	 * @param Notice
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(Advice advice, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(!StringUtil.isNullOrEmpty(advice.getKeywords())) {
      if(!StringUtil.isNullOrEmpty(advice.getKeyType())) {
        dc.add(Restrictions.like(advice.getKeyType(), advice.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("falseCreateTime", date[0],date[1]));
	  }
	  if(!StringUtil.isNullOrEmpty(advice.getStatus())){
	    dc.add(Restrictions.eq("status", advice.getStatus()));
	  }
	  if(!StringUtil.isNullOrEmpty(advice.getUserid())){
      dc.add(Restrictions.eq("userid", advice.getUserid()));
    }
	  if(!StringUtil.isNullOrEmpty(advice.getSortType())) {
	    dc.addOrder(Order.desc("sortType"));
	  }
	  dc.addOrder(Order.desc("falseCreateTime"));
		return dc;
	}
	
	//查id，depars
	public Map<String, Object> getids() {
		Map<String, Object> map = new HashMap<String, Object>();
	    String sql = "SELECT id FROM advice WHERE transforms IS NULL";
	    SQLQuery query = super.em().createSQLQuery(sql.toString());
	    map.put("dataList", query.list());
	    return map;
	  }
	
	public Map<String, Object> getdeps() {
		Map<String, Object> map = new HashMap<String, Object>();
	    String sql = "SELECT depars FROM advice WHERE transforms IS NULL";
	    SQLQuery query = super.em().createSQLQuery(sql.toString());
	    map.put("dataList", query.list());
	    return map;
	  }
	
	public Map<String, Object> getTop3() {
		Map<String, Object> map = new HashMap<String, Object>();
	    String sql = "SELECT * FROM advice ORDER BY falseCreateTime DESC LIMIT 0,3";
	    SQLQuery query = super.em().createSQLQuery(sql.toString());
	    map.put("dataList", query.list());
	    return map;
	  }
	
	/**
	 * 按条件查询
	 * @param Notice
	 * @return
	 */
	public List<Advice> querys(Advice advice, String... date) {
		if(advice == null) return super.findAll();
		//return super.find(createFilter(Notice, Notice.getiDisplayLength() > 0, date));
		if(advice.getiDisplayLength() > 0){
		  return super.find(createFilters(advice, date),advice.getiDisplayStart(),advice.getiDisplayLength());
		}
		return super.find(createFilters(advice, date));
	}

	public Long counts(Advice advice, String... date) {
		return super.count(createFilters(advice, date));
	}
	
	/**
	 * 拼装条件
	 * @param Notice
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilters(Advice advice, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("falseCreateTime", date[0],date[1]));
	  }
	  if(!StringUtil.isNullOrEmpty(advice.getStatus())){
		    dc.add(Restrictions.eq("status", advice.getStatus()));
		  }
	  dc.addOrder(Order.desc("falseCreateTime"));
		return dc;
	}
	
	
}
